
import GlslPainter from './glsl_painter';

function createPendant(context) {
  return new context.Pendant({
    require : ['three'],
    painter : null,
    // 会被覆盖的
    sendStatus() {},
    init(libs, args) {
      this.painter = new GlslPainter(context, {
        ...args,
        THREE : libs.three
      });
    },
		collectInfo() {
			return this.painter ? this.painter.collectInfo() : {};
    },
    onMessage (name, arg) {
      switch (name) {
      case 'set_speed': {
        // speed 范围 : -10~10
        if (typeof(arg.speed) != 'number') {
          break;
        }
        let speed = arg.speed < -10 ? -10 : arg.speed > 10 ? 10 : arg.speed;
        this.painter.setSpeed(speed);
      } break;
      case 'pause': {
        this.painter.pause();
      } break;
      case 'set_mouse': {
        // x 范围 0~1, y 范围 0~1
        if (typeof(arg.x) != 'number' || typeof(arg.y) != 'number') {
          break;
        }
        let x = arg.x < 0 ? 0 : arg.x > 1 ? 1 : arg.x;
        let y = arg.y < 0 ? 0 : arg.y > 1 ? 1 : arg.y;
        this.painter.setMouse(x, y);
      } break;
      default: return;
      }
      this.sendStatus();
    },
  });
}

export default createPendant;
